package com.huawei.windows;

import com.huawei.file.Result;
import com.huawei.file.UserFileimpl;
import com.huawei.object.Computer;
import com.huawei.object.User;
import com.huawei.service.GameRuleImpl;
import com.huawei.service.SCIimpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class PlayWindows implements Windows {
    private int playinground = 0;

    private void setround() {
        playinground++;
    }

    @Override
    public void GameWindow() {
        //准备
        UserFileimpl userFileimpl=new UserFileimpl();
        //窗口
        JFrame gameFrame = new JFrame("石 头 剪 刀 布");
        gameFrame.setSize(400, 400);
        gameFrame.setLocationRelativeTo(null);
        gameFrame.setResizable(false);
        gameFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //标签
        JLabel mainLabel = new JLabel("主 菜 单");
        mainLabel.setBounds(140, 10, 150, 150);
        mainLabel.setFont(new Font(null, Font.PLAIN, 30));
        //按钮
        //对局记录查看
        JButton recordButton =new JButton("记录");
        recordButton.setBounds(240, 250, 100, 50);
        recordButton.setFont(new Font(null, Font.PLAIN, 20));
        recordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    RecordWindow(userFileimpl.resultOut());
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(gameFrame, "无对局记录！！！！", "警告", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        //规则
        JButton ruleButton = new JButton("规则");
        ruleButton.setBounds(50, 250, 100, 50);
        ruleButton.setFont(new Font(null, Font.PLAIN, 20));
        ruleButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gameFrame, "还有人不知道剪刀石头布的规则吗？？", "游戏规则", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        //pvc
        JButton pvcButton = new JButton("PVC");
        pvcButton.setBounds(50, 150, 100, 50);
        pvcButton.setFont(new Font(null, Font.PLAIN, 20));
        pvcButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int round = new SCIimpl().change(JOptionPane.showInputDialog(gameFrame, "输入对局数:", "输入非数字可能报错"));
                if (round == -1) {
                    JOptionPane.showMessageDialog(gameFrame, "请输入数字(正确数字)！！！！", "警告", JOptionPane.WARNING_MESSAGE);
                } else if(round !=0){
                    User player=new User();
                    player.setName(JOptionPane.showInputDialog(gameFrame, "输入昵称:","player"));
                    if(player.getName()!=null){
                    new PlayWindows().PlayWindow(round,player.getName());}
                }
            }
        });
        //pvp
        JButton pvpButton = new JButton("PVP");
        pvpButton.setBounds(240, 150, 100, 50);
        pvpButton.setFont(new Font(null, Font.PLAIN, 20));
        pvpButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(gameFrame, "敬请期待", "通告", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        //画板
        JPanel mainUI = new JPanel(null);
        mainUI.add(mainLabel);
        mainUI.add(recordButton);
        mainUI.add(ruleButton);
        mainUI.add(pvcButton);
        mainUI.add(pvpButton);
        //尾部
        gameFrame.setContentPane(mainUI);
        gameFrame.setVisible(true);
    }

    @Override
    public void RecordWindow(String record) {
        //准备
        //窗口
        JFrame jf =new JFrame("对局记录");
        jf.setSize(380,380);
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //文本区
        JTextArea textArea =new JTextArea();
        textArea.setLineWrap(true);
        textArea.setFont(new Font(null,Font.PLAIN,18));
        textArea.setText(record);
        textArea.setEditable(false);
        textArea.setSize(380,330);
        //按钮
        JButton emptyButton=new JButton("清空");
        emptyButton.setBounds(50, 250, 100, 50);
        emptyButton.setFont(new Font(null, Font.PLAIN, 20));
        emptyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int choose=JOptionPane.showConfirmDialog(jf, "确认删除？", "提示", JOptionPane.YES_NO_OPTION);
                if(choose==0) {
                    new UserFileimpl().clearInfoForFile();
                    jf.dispose();
                }
            }
        });
        //滚动面板
        JScrollPane scrollPane =new JScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        //
        JPanel jPanel=new JPanel(new BorderLayout());
        jPanel.add(scrollPane,BorderLayout.NORTH);
        jPanel.add(scrollPane,BorderLayout.CENTER);
        jPanel.add(emptyButton,BorderLayout.SOUTH);
        //尾部
        jf.setContentPane(jPanel);
        jf.setVisible(true);
    }

    @Override
    public void PlayWindow(int round,String playerName) {
        //准备
        playinground = 0;
        final boolean[] overNormal = {false};
        UserFileimpl userFile =new UserFileimpl();
        GameRuleImpl gameRule = new GameRuleImpl();
        Computer computer = new Computer();
        User player = new User();
        //窗口
        JFrame playingFrame = new JFrame("第 " + playinground + " 局");
        playingFrame.setSize(400, 400);
        playingFrame.setLocationRelativeTo(null);
        playingFrame.setResizable(false);
        playingFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        //标签
        JLabel playingLabel = new JLabel("你的分数:对手分数");
        playingLabel.setBounds(70, 0, 300, 50);
        playingLabel.setFont(new Font(null, Font.PLAIN, 30));

        JLabel playingLabel1 = new JLabel(gameRule.getWin() + ":" + gameRule.getLose());
        playingLabel1.setBounds(170, 50, 150, 50);
        playingLabel1.setFont(new Font(null, Font.PLAIN, 30));
        //按钮
        //剪刀(2)
        JButton jiandao = new JButton("剪刀");
        jiandao.setFont(new Font(null, Font.PLAIN, 20));
        jiandao.setBounds(50, 150, 100, 50);
        jiandao.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                player.setOperations(2);
                setround();
                PlayCoinWindow(gameRule.judge(player.getOperations() - computer.getOperations()));
                playingLabel1.setText(gameRule.getWin() + ":" + gameRule.getLose());
                playingFrame.setTitle("第 " + playinground + " 局");
                if (gameRule.gameIsOver(round - playinground, round, gameRule.getWin(), gameRule.getLose())) {
                    overNormal[0] =true;
                    playingFrame.dispose();
                }
            }
        });
        //石头(1)
        JButton shitou = new JButton("石头");
        shitou.setFont(new Font(null, Font.PLAIN, 20));
        shitou.setBounds(240, 150, 100, 50);
        shitou.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                player.setOperations(1);
                setround();
                PlayCoinWindow(gameRule.judge(player.getOperations() - computer.getOperations()));
                playingLabel1.setText(gameRule.getWin() + ":" + gameRule.getLose());
                playingFrame.setTitle("第 " + playinground + " 局");
                if (gameRule.gameIsOver(round - playinground, round, gameRule.getWin(), gameRule.getLose())) {
                    overNormal[0] =true;
                    playingFrame.dispose();
                }
            }
        });
        //布(3)
        JButton bu = new JButton("布");
        bu.setFont(new Font(null, Font.PLAIN, 20));
        bu.setBounds(145, 250, 100, 50);
        bu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                player.setOperations(3);
                setround();
                PlayCoinWindow(gameRule.judge(player.getOperations() - computer.getOperations()));
                playingLabel1.setText(gameRule.getWin() + ":" + gameRule.getLose());
                playingFrame.setTitle("第 " + playinground + " 局");
                if (gameRule.gameIsOver(round - playinground, round, gameRule.getWin(), gameRule.getLose())) {
                    overNormal[0] =true;
                    playingFrame.dispose();
                }
            }
        });
        //画板
        JPanel playingPanel = new JPanel(null);
        playingPanel.add(playingLabel);
        playingPanel.add(playingLabel1);
        playingPanel.add(jiandao);
        playingPanel.add(shitou);
        playingPanel.add(bu);
        //尾部
        playingFrame.setContentPane(playingPanel);
        playingFrame.setVisible(true);
        //窗口关闭
        playingFrame.addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {//是0否1
                int choose = JOptionPane.showConfirmDialog(playingFrame, "确认退出？(中途退出无法记录对局)", "提示", JOptionPane.YES_NO_OPTION);
                if (choose == 0) playingFrame.dispose();
            }

            @Override
            public void windowClosed(WindowEvent e) {
                if(overNormal[0]){
                    int wl= ResultWindow(gameRule.getWin() - gameRule.getLose());
                    Result result= userFile.addResult(playerName,wl,gameRule.getWin(),round);
                    userFile.resultIn(result);
                }
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }
    @Override
    public int ResultWindow(int result) {
        JFrame jf = new JFrame();
        if (result > 0) {JOptionPane.showMessageDialog(jf, "你赢了！！", "游戏结果", JOptionPane.INFORMATION_MESSAGE);return 1;}
        else if (result == 0) {JOptionPane.showMessageDialog(jf, "平局！！", "游戏结果", JOptionPane.INFORMATION_MESSAGE);return  2;}
        else {JOptionPane.showMessageDialog(jf, "你输了！！", "游戏结果", JOptionPane.INFORMATION_MESSAGE);return 3;}
    }

    @Override
    public void PlayCoinWindow(int winlose) {
        JFrame jf = new JFrame();
        switch (winlose) {
            case 1:
                JOptionPane.showMessageDialog(jf, "你得分了！！", "回合结果", JOptionPane.INFORMATION_MESSAGE);
                break;
            case 2:
                JOptionPane.showMessageDialog(jf, "无人得分！！", "回合结果", JOptionPane.INFORMATION_MESSAGE);
                break;
            case 3:
                JOptionPane.showMessageDialog(jf, "对手得分！！", "回合结果", JOptionPane.INFORMATION_MESSAGE);
                break;
        }
    }
}